//
//  SISSUploadTenancyContractViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/5.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadTenancyContractViewController.h"
#import "SISSRentOutProgressModel.h"
#import "SISSRentOutProgressRecordInfoModel.h"
#import "SISSTenantProgressModel.h"
#import "SISSTenantProgressRecordInfoModel.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSTenancyContractModel.h"

@interface SISSUploadTenancyContractViewController ()

@end

@implementation SISSUploadTenancyContractViewController

- (void)viewDidLoad {
    
    self.isHiddenDateChooseView = YES;
    
    [super viewDidLoad];
    
    self.title = @"物业租赁及经纪合同";
    
    if (self.tenancyContractModel) {
        self.uploadImageView.imageUrlArr = self.tenancyContractModel.image.mutableCopy;
        self.remarkView.rightTextField.text = self.tenancyContractModel.otherdesc;
    }
}

- (void)confirmButtonClickAction {
    
    [self.view endEditing:YES];
    
    if (!self.tenancyContractModel && (!self.uploadImageView.images || self.uploadImageView.images.count == 0)) {
        [SISSTools showSVWithTitle:@"请选择合同电子档图片"];
        return;
    }
    NSString *albpId = self.rentOutProgressModel ? self.rentOutProgressModel.albpId : self.tenantProgressRecordInfoModel.albpId;
   NSString *arbpId = self.tenantProgressModel ? self.tenantProgressModel.arbpId : self.rentOutProgressRecordInfoModel.arbpId;
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
    NSInteger leaseType = self.rentOutProgressModel ? 1 : self.tenantProgressRecordInfoModel.tradeBusinessType;
    NSInteger rentType = self.tenantProgressModel ? 1 : self.rentOutProgressRecordInfoModel.tradeBusinessType;
    if (rentType == 2) arbpId = self.rentOutProgressRecordInfoModel.arrId;
    if (leaseType == 2) albpId = self.tenantProgressRecordInfoModel.alrId;
    
    if (self.tenancyContractModel) {
        NSDictionary *parameters = @{
                                     @"id" : self.tenancyContractModel.ID,
                                     @"albpId" : albpId,
                                     @"arbpId" : arbpId,
                                     @"leaseType" : @(leaseType),
                                     @"rentType" : @(rentType),
                                     @"otherdesc" : otherdesc
                                     };
        [kNetworkManager updateLeaseContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
            
            if ([responseObject[@"code"] integerValue] == 200) {
                
                [self pushToSuccessViewWithPopBackClassType:self.rentOutProgressModel ? SISSProgressPopBackClassTypeRentOut : SISSProgressPopBackClassTypeTenant];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else {
        NSDictionary *parameters = @{
                                     @"albpId" : albpId,
                                     @"arbpId" : arbpId,
                                     @"lease_type" : @(leaseType),
                                     @"rent_type" : @(rentType),
                                     @"otherdesc" : otherdesc
                                     };
        if (self.rentOutProgressModel) {
            [kNetworkManager insertLeaseContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    
                    [self pushToSuccessViewWithPopBackClassType: SISSProgressPopBackClassTypeRentOut];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        } else if (self.tenantProgressModel) {
            [kNetworkManager insertTenantProgressTenancyContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    
                    [self pushToSuccessViewWithPopBackClassType:SISSProgressPopBackClassTypeTenant];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
        
    }
    
}


- (void)initConstraint {
    [super initConstraint];
    self.contractNoView.hidden = YES;
    WeakSelf
    [self.remarkView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.containerView).offset(globelMargin);
        make.left.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(5.5 * globelMargin);
    }];
    
}

@end
